home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / doc.zoo / doc / usrman / croff / mktable.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  3.5 KB  |  137 lines

  1. /*                        Copyright (c) 1988 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: mktable.c,v 1.1 88/07/07 10:12:17 sau Exp $
  9.     $Source: /tmp/mgrsrc/doc/usrman/croff/RCS/mktable.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/doc/usrman/croff/RCS/mktable.c,v $$Revision: 1.1 $";
  12.  
  13. /* make a compile time hash table  (SAU)
  14.  *
  15.  * usage: mktable <name> <size>
  16.  *
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include "hash.h"
  21.  
  22. #define SIZE        71
  23. #define MIN_SIZE    5
  24.  
  25. char key[80];
  26. char value[80];
  27.  
  28. main(argc,argv)
  29. int argc;
  30. char **argv;
  31.    {
  32.    register int i,code, count = 0;
  33.    char *malloc(), *sprintf();
  34.    TABLE **table;            /* place to build hash table into */
  35.    register TABLE *list, *next;
  36.    char *name;                /* name of table */
  37.    char *prog = *argv;
  38.    int len=0, size;
  39.    char tmp[80];
  40.  
  41.    int r_flag = 0;            /* reverse sense of keyword-value */
  42.    int n_flag = 0;            /* no values */
  43.    
  44.    for(;argc>1 && *argv[1]=='-';argv++,argc--) 
  45.       switch(argv[1][1]) {
  46.          case 'r': r_flag++; break;
  47.          case 'n': case '1': n_flag++; *value='\0'; break;
  48.          default : fprintf(stderr,"%s: flag %s ignored\n",prog,argv[1]);
  49.          }
  50.  
  51.    if (argc < 3) {
  52.       fprintf(stderr,"usage: %s [-r -n] <name> <number of buckets>\n",prog);
  53.       exit(1);
  54.       }
  55.  
  56.    name = argv[1];
  57.  
  58.    if ((size = atoi(argv[2])) < MIN_SIZE) 
  59.       size = SIZE;
  60.  
  61.    if ((table = (TABLE **) malloc(size * sizeof(TABLE))) == NULL) {
  62.       perror("Can't alloc space for table");
  63.       exit(1);
  64.       }
  65.  
  66.    bzero(table,size*sizeof(TABLE));
  67.  
  68.    /* build the hash table */
  69.  
  70.    while(1) {
  71.       if (n_flag)
  72.          code = scanf("%s\n",key);
  73.       else if (r_flag)
  74.          code = scanf("%s %s\n",value,key);
  75.       else
  76.          code = scanf("%s %s\n",key,value);
  77.  
  78.       if (code == EOF)
  79.          break;
  80.       add_entry(table,size,key);
  81.       put_entry(table,size,key,value);
  82.       count++;
  83.       }
  84.  
  85.    /* print out data */
  86.  
  87.    printf("/* hash table: %d items in %d buckets */\n\n",count,size);
  88.    printf("#include <stdio.h>\n");
  89.    printf("#include \"hash.h\"\n\n");
  90.    printf("#define SIZE_%s    %d\n\n",name,size);
  91.    printf("struct table_entry %s_data[] = {\n",name);
  92.  
  93.    for(count=0,i=0;i<size;i++) {
  94.       for(code=0,list=table[i];list != (TABLE *) 0; list = next,count++) {
  95.          next = list->next;
  96.          printf("   {\"%s\", \"%s\", %d, 0x%x, %s},",
  97.                list->name, list->value, list->count, HASH_STATIC,
  98.                next ? sprintf(tmp,"&%s_data[%d]",name,count+1) : "NULL");
  99.          if (code++ == 0)
  100.             printf("    /*  %d */\n",i);
  101.          else
  102.             printf("\n");
  103.          }
  104.       }
  105.    printf("   };\n\n");
  106.  
  107.    /* print out hash table */
  108.  
  109.    printf("/* hash table: %d items */\n\n",size);
  110.    printf("struct table_entry *%s[] = {\n  ",name);
  111.  
  112.    for(len=2,code=count=i=0;i<size;i++) {
  113.       for(list=table[i]; list != (TABLE *) 0; list = list->next) {
  114.          count++;
  115.          code++;
  116.          }
  117.       if (code) {
  118.          sprintf(tmp," &%s_data[%d],",name,count-code);
  119.          code = 0; 
  120.          }
  121.       else
  122.          sprintf(tmp," NULL,");
  123.  
  124.       len += strlen(tmp);
  125.  
  126.       if (len > 78) {
  127.          printf("\n  ");
  128.          len = 2 + strlen(tmp);
  129.          }
  130.       
  131.       printf("%s",tmp);
  132.       }
  133.    if (len)
  134.       printf("\n  ");
  135.    printf(" };\n\n");
  136.    }
  137.